//
//  BJSeriesView.m
//  SZBank
//
//  Created by rimi on 16/7/16.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BJSeriesView.h"
#import "TableImageView.h"
@interface BJSeriesView ()<UIWebViewDelegate>
{
    CGFloat frameWidth;/**< 自身宽度 */
    CGFloat frameHeight;/**< 自身高度 */
}

@property (nonatomic,strong)UIWebView *webView;/**< 网络视图 */
@end
@implementation BJSeriesView

- (instancetype)initWithFrame:(CGRect)frame withDataString:(NSString *)dataString tableImageArray:(NSArray *)tableImageArray
{
    self = [super initWithFrame:frame];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        [self.webView loadHTMLString:dataString baseURL:nil];
        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, tableImageArray.count * frameHeight * 0.7);
        
            [self addSubview:self.scrollView];
            [self addSubview:self.webView];
        [self addImageTableViewWithDataArray:tableImageArray];

        
    }
    return self;
}
#pragma mark -- actions
/**
 *  添加tableImageView在滚动视图上面
 */
- (void)addImageTableViewWithDataArray:(NSArray *)dataArray{
     NSMutableDictionary *tableDict = [NSMutableDictionary dictionary];
    NSMutableArray *array_ID = [NSMutableArray array];
    for (NSDictionary * dict in dataArray) {
        NSString *titleString = dict[@"NAME"];
        
        NSString *investmemDayString = [NSString stringWithFormat:@"%@天",dict[@"INVESTMENTPERIOD"]];
        //预期年收益
        NSString *investmemString;
        if (![dict[@"YIELDLOW"] isEqualToString:dict[@"YIELDUP"]]) {
            investmemString  = [NSString stringWithFormat:@"%@%@~%@%@",dict[@"YIELDLOW"],@"%",dict[@"YIELDUP"],@"%"];
        }else{
            investmemString = [NSString stringWithFormat:@"%@%@",dict[@"YIELDUP"],@"%"];
        }
        //
        NSString *INVESTMENTTHRESHOLD = [NSString stringWithFormat:@"%@万",dict[@"INVESTMENTTHRESHOLD"]] ;
        NSString *SUBSTARTTIME = [dict[@"SUBSTARTTIME"] substringToIndex:10];
        NSString *SUBENDTIME = [dict[@"SUBENDTIME"] substringToIndex:10];;
        NSString *INTERESTSTARTDATE = [dict[@"INTERESTSTARTDATE"] substringToIndex:10];
        NSString *INTERESTENDDATE = [dict[@"INTERESTENDDATE"] substringToIndex:10];
        
        //加入数组
        NSArray *array1 = @[titleString,investmemDayString,investmemString,INVESTMENTTHRESHOLD,SUBSTARTTIME,SUBENDTIME,INTERESTSTARTDATE,INTERESTENDDATE];
        [array_ID addObject:dict[@"ID"]];
        //加入字典
        [tableDict setObject:array1 forKey:dict[@"ID"]];
       
        
    }
    
    NSMutableDictionary *finalTableDictionay = [NSMutableDictionary dictionary];
    NSInteger tableStyle = 0;
    for (int i = 0; i < array_ID.count; i ++) {
        
        if ([dataArray[i][@"ID"] isEqualToString:array_ID[i]]) {
            [finalTableDictionay setObject:dataArray[i] forKey:@"dataArray"];
        }
        if (((NSArray *)dataArray[i][@"YIELDDESC"]).count != 0) {
            tableStyle = 1;
        }else{
            tableStyle = 0;
        }
        
                //创建视图
        TableImageView *tableImageView = [[TableImageView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * 0.15, self.scrollView.frame.size.height * 0.05 + i * self.scrollView.frame.size.height * 0.7 , self.scrollView.frame.size.width * 0.75, self.scrollView.frame.size.height * 0.6) withDataDictionary:@{@"dataSource":finalTableDictionay,@"labels":tableDict[array_ID[i]]} tableStyle:tableStyle];

        
        //回传接收
        tableImageView.tableViewBlock = ^(NSDictionary *dictionary){
            if (self.bjSeriesViewBlock) {
                
                self.bjSeriesViewBlock(dictionary);
            }
        };
        
        
        [self.scrollView addSubview:tableImageView];

    }
}


#pragma mark -- UIWebViewDelegate
// 是否允许开始加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //修改webView显示内容的字体大小
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100 %'"];//修改百分比即可
}
#pragma mark -- getters


- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, frameWidth * 0.6, frameHeight)];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;

        _webView.scrollView.showsVerticalScrollIndicator = YES;
        
   
    }
    return _webView;
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.webView.frame), 0, frameWidth - self.webView.frame.size.width, frameHeight)];
        _scrollView.showsVerticalScrollIndicator = YES;
     
        
    }
    return _scrollView;
}


@end
